<?php
/**
 * This class maps a given class name to the actual PHP file so
 * it can be included easily.
 */
    class LookupClassLocation {
        public static $root = "msprotdb";
        public static $classLocMap = array(
            "DatabaseFactory" => "scripts/dba/dbFac.inc",
            "LookupClassLocation" => "scripts/lookupClasses.inc",
            "ParseAccessions" => "scripts/parseAccessions.inc",
            "Database" => "scripts/dba/%DB%/database.inc",
            "User" => "scripts/dba/%DB%/user.inc",
            "Users" => "scripts/dba/%DB%/users.inc",
            "Group" => "scripts/dba/%DB%/group.inc",
            "Groups" => "scripts/dba/%DB%/groups.inc",
            "Security" => "layout/security.inc",
            "Install" => "scripts/install/install.inc",
            "Installable" => "scripts/install/installableInterface.inc"
        );
        /**
         * Given the case sensitive class name, the location of
         * the actual path to the include file is provided in return.
         * @param <type> $className
         * @return <type>
         */
        public static function lookup($className) {
            $fp = __FILE__;
            $pos = strpos($fp,$root);
            if($pos >= 0) {
                $pre = substr($fp,0,$pos+strlen($root)) . "/";
                $path = $pre . $this->classLocMap[$className];
                if(strpos("%DB%",$path) > 0)
                    return(false);
                return($path);
            } else
                return($this->classLocMap[$className]);
        }
        /**
         * Given the case sensitive class name, the location of
         * the actual path to the include file is provided in return.
         * @param <type> $className
         * @param <type> $dbms the currently used DBMS (and thus the subdirectory under dba)
         * @return <type> the concrete path to the file containing the class.
         */
        public static function lookup($className, $dbms) {
            $fp = __FILE__;
            $pos = strpos($fp,$root);
            if($pos >= 0) {
                $pre = substr($fp,0,$pos+strlen($root)) . "/";
                $path = $pre . $this->classLocMap[$className];
                $path = str_replace("%DB%", $dbms, $path);
                return($path);
            } else
                return($this->classLocMap[$className]);
        }
        /**
         * This function determines the distance to the root file given a path
         * @param <type> $path
         * @return a string containing "../" to reach the root from the current location.
         */
        public static function getDistanceToRoot($path) {
            $distance = "";
            $dirArr = split("/",$path);
            if(strpos($dirArr[count($dirArr)-1]) === true) {
                array_pop($dirArr);
            } else {
                if($dirArr[count($dirArr)-1] != $root)
                    $distance .= "../";
                else
                    return($distance);
            }
            for($i=count($dirArr)-2; $i>=0; $i--) {
                if($dirArr[count($dirArr)-1] != $root)
                    $distance .= "../";
                else
                    return($distance);
            }
        }
    }
?>